package com.core.shared;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created with IntelliJ IDEA.
 * User: sunk
 * Date: 15-1-9
 * Time: 下午2:20
 * To change this template use File | Settings | File Templates.
 */
public class CommonUtils {

    //匹配中文字母数字和简单特殊字符，排除英文引号、尖括号
    public static final String simpleReg = "[\\u4e00-\\u9fa5\\w~`?？！,。《》【】｛｝（）—-￥…、：；‘’“”·~，\\-+=()*&^%$#@!{}\\[\\]:;|\\\\.]*";
    public static final String commonReg = "[^\\u4e00-\\u9fa5\\w]";
    public static final String bigNumber = "[^\\uFF10-\\uFF19]"; //全角数字
    public static final String bigWords = "[^\\uff21-\\uff5a]"; //全角字母,包含大小写  大写：\uff21-\uff3a，小写：\uff41-\uff5a
    private static final Pattern pattern = Pattern.compile(commonReg);

    public static boolean isEmpty(String value) {
        if (value == null || value.trim().length() == 0) {
            return true;
        }
        return false;
    }

    public static boolean isNotEmpty(String value) {
        if (value == null || value.trim().length() == 0) {
            return false;
        }
        return true;
    }

    /**
     * 替换utf-8里特有的空格编码,否则会变成问号
     *
     * @param content
     * @return
     */
    public static String replaceUTF8SpecialSpaces(String content) {
        if (isEmpty(content)) {
            return null;
        }
        try {
            byte[] space = new byte[]{(byte) 0xC2, (byte) 0xA0};
            String utfSpace = new String(space, "utf-8");
            content = content.replace(utfSpace, "&nbsp;");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return content;
    }

    /**
     * 对二维数组进行排列组合
     *
     * @param data 要排列的元数据
     * @param n    元数据长度
     * @return
     */
    public static List<String> combine(Object[][] data, int n) {
        if (n > data.length) {
            n = data.length;
        }
        int[] idx = new int[data.length];
        for (int i = 0; i < data.length; i++) {
            if (i < data.length - n) {
                idx[i] = 0;
            } else {
                idx[i] = 1;
            }
        }
        List<String> list = new ArrayList<>();
        while (idx[0] < 2) {
            int cnt = 0, dig = 0;
            int[] gp = new int[n];
            for (int i = 0; i < idx.length; i++) {
                if (idx[i] == 1) {
                    if (i < n) {
                        dig++;
                    }
                    if (cnt < n) {
                        gp[cnt] = i;
                    }
                    cnt++;
                }
            }
            if (n == cnt) {
                int[] subIdx = new int[n];
                Arrays.fill(subIdx, 0);
                while (subIdx[0] < data[gp[0]].length) {
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < n; i++) {
                        sb.append(data[gp[i]][data[gp[i]].length - subIdx[i] - 1]).append("-");
                    }
                    list.add(0, sb.toString());
                    subIdx[n - 1]++;
                    for (int i = n - 1; i > 0; i--) {
                        if (subIdx[i] == data[gp[i]].length) {
                            subIdx[i] = 0;
                            subIdx[i - 1]++;
                        } else {
                            break;
                        }
                    }
                }
            }
            if (dig == n) {
                break;
            }
            idx[data.length - 1]++;
            for (int i = data.length - 1; i > 0; i--) {
                if (idx[i] == 2) {
                    idx[i] = 0;
                    idx[i - 1]++;
                } else {
                    break;
                }
            }
        }
        int count = 0;
        for (String s : list) {
            System.out.printf("%s,", s);
            count++;
            if (count % 10 == 0) {
                System.out.println();
            }
        }
        System.out.printf("\ncount=%d\n", count);
        return list;
    }

    /**
     * 正则匹配替换字符串
     * 默认替换除中文、字母、数字以外的所有字符
     *
     * @param content     被替换的字符串
     * @param replaceMent 需要替换的内容
     * @return
     */
    public static String handleByReg(String content, String replaceMent) {
        if (isEmpty(content)) {
            return "";
        }
        Matcher matcher = pattern.matcher(content);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, replaceMent);
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    /**
     * 正则匹配替换字符串
     *
     * @param content        被替换的字符串
     * @param replaceMent    需要替换的内容
     * @param specialPattern 指定的正则
     * @return
     */
    public static String handleByReg(String content, String replaceMent, Pattern specialPattern) {
        if (isEmpty(content)) {
            return "";
        }
        if (specialPattern == null) {
            specialPattern = pattern;
        }
        Matcher matcher = specialPattern.matcher(content);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, replaceMent);
        }
        matcher.appendTail(sb);
        return sb.toString();
    }


}
